package com.facecard;

import java.net.URI;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

public class OwnerInfo extends Activity {
	EditText txtFullName;
	EditText txtEmail;
	EditText txtHoroscope;
	EditText txtHobbies;
	EditText txtPhone;
	EditText txtPic;
	ImageButton btnBrowserTemplate;
	ImageButton btnAvatar;

	private int mintIndexTemplate = -1;
	private String mstrAvatarPath = "";

	SharedPreferences prefs;

	public static final String PREF_FULLNAME = "PREF_FULLNAME";
	public static final String PREF_EMAIL = "PREF_EMAIL";
	public static final String PREF_HOROSCOPE = "PREF_HOROSCOPE";
	public static final String PREF_HOBBIES = "PREF_HOBBIES";
	public static final String PREF_PHONE = "PREF_PHONE";
	public static final String PREF_PIC = "PREF_PIC";
	public static final String PREF_TEMPLATE = "PREF_TEMPLATE";

	private final int INT_BROW_TEMPLATE = 0;
	private final int INT_CHOICE_AVATAR = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.ownerinfo);

		txtFullName = (EditText)findViewById(R.id.txt_fullname);
		txtEmail = (EditText)findViewById(R.id.txt_email);
		txtHoroscope = (EditText)findViewById(R.id.txt_horoscope);
		txtHobbies = (EditText)findViewById(R.id.txt_hobbies);
		txtPhone = (EditText)findViewById(R.id.txt_phone);
		//txtPic = (EditText)findViewById(R.id.txt_picture);
		//txtTemplate = (TextView)findViewById(R.id.txtTemplate);

		populateSpinners();

		prefs = getSharedPreferences(Commons.STR_PREFERENCE_NAME, Activity.MODE_PRIVATE);

		btnBrowserTemplate = (ImageButton) findViewById(R.id.browsertmpButton);
		btnBrowserTemplate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(OwnerInfo.this,
						ChooseTemplate.class);
				startActivityForResult(intent, INT_BROW_TEMPLATE);

			}
		});

		btnAvatar = (ImageButton) findViewById(R.id.avatarButton);
		btnAvatar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, INT_CHOICE_AVATAR); 
			}
		});

		Button previewButton = (Button) findViewById(R.id.previewButton);
		previewButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(OwnerInfo.this.getBaseContext(), CardView.class);
				startActivity(i);
			}
		});

		Button okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				savePreferences();
				setResult(RESULT_OK);
				finish();
			}
		});

		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});

		updateUIFromPreferences();
	}

	private void savePreferences() {
		Editor editor = prefs.edit();
		editor.putString(PREF_FULLNAME, txtFullName.getText().toString());
		editor.putString(PREF_EMAIL, txtEmail.getText().toString());
		editor.putString(PREF_HOROSCOPE, txtHoroscope.getText().toString());
		editor.putString(PREF_HOBBIES, txtHobbies.getText().toString());
		editor.putString(PREF_PHONE, txtPhone.getText().toString());
		editor.putString(PREF_PIC, mstrAvatarPath);
		editor.putInt(PREF_TEMPLATE, getIndexTemplate());
		editor.commit();
	}

	/** Update the UI state based on the saved preferences */
	private void updateUIFromPreferences() {
		txtFullName.setText(prefs.getString(PREF_FULLNAME, ""));
		txtEmail.setText(prefs.getString(PREF_EMAIL, ""));
		txtHoroscope.setText(prefs.getString(PREF_HOROSCOPE, ""));
		txtHobbies.setText(prefs.getString(PREF_HOBBIES, ""));
		txtPhone.setText(prefs.getString(PREF_PHONE, ""));
		setAvatarPath(prefs.getString(PREF_PIC, ""));
		setIndexTemplate(prefs.getInt(PREF_TEMPLATE, 0));
		/*		txtPic.setText(prefs.getString(PREF_PIC, ""));
		txtTemplate.setText(prefs.getString(PREF_TEMPLATE, ""));
		 */	}

	public synchronized void onActivityResult(final int requestCode,
			int resultCode, final Intent data) {

		if (resultCode == Activity.RESULT_OK) {
			switch(requestCode)
			{
			case INT_BROW_TEMPLATE:
				setIndexTemplate(data.getIntExtra(ChooseTemplate.RESULT_PATH,-1));
				break;
			case INT_CHOICE_AVATAR:
				setAvatarPath(data.getData());
				break;
			}

		} else if (resultCode == Activity.RESULT_CANCELED) 
		{
		}

	}

	/** Populate the spinners with the external array values */
	private void populateSpinners() {  
	}

	int getIndexTemplate()
	{
		return  mintIndexTemplate;
	}

	void setIndexTemplate(int pintIdx)
	{
		mintIndexTemplate = pintIdx;
		if(pintIdx>-1)
			btnBrowserTemplate.setImageResource(Commons.INT_TEMPLATES[pintIdx]);				
	}

	String getAvatarPath()
	{
		return mstrAvatarPath;
	}

	void setAvatarPath(Uri selectedImageUri)
	{
		//OI FILE Manager
		mstrAvatarPath = selectedImageUri.toString();

		btnAvatar.setImageURI(selectedImageUri);
	}

	void setAvatarPath(String pstrPatch)
	{
		//OI FILE Manager
		mstrAvatarPath = pstrPatch;

		btnAvatar.setImageURI(Uri.parse(pstrPatch));
	}
}
